﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using BusinnessLogic;

namespace scrumbench
{
    public partial class SprintviewForm : Form
    {
        public SprintviewForm()
        {
            InitializeComponent();
        }

        public SprintviewForm(int sprintid, string sprinttitel)
        {
            InitializeComponent();

            #region MakeGraph
            //First Method with use of DATAPOINTS: NOT THE BEST METHOD
            //Sprintview sprinter = new Sprintview();
            //List<ChartPoint> chartptlist = sprinter.CreateChartArray(sprintid);

            //foreach (ChartPoint chrpt in chartptlist)
            //{
            //    //Graph 1 with the totals of (EWL - RWL)
            //    DataPoint dp = new DataPoint(this.SprintBurnDownChart.Series[0]);
            //    dp.YValues[0] = chrpt.Ywaarde;
            //    //dp.XValue = double.Parse(chrpt.Datum.ToString());
            //    dp.LabelToolTip = chrpt.Ewl + " - " + chrpt.Rwl;
            //    //dp.Label = chrpt.Ewl + " - " + chrpt.Rwl;
            //    this.SprintBurnDownChart.Series[0].Points.Add(dp);

            //    //Graph 2 with the totals of Estimated Workload
            //    this.SprintBurnDownChart.Series[1].Points.Add(chrpt.Ewl);


            //    //Graph 3 with the total of Real Workload
            //    this.SprintBurnDownChart.Series[2].Points.Add(chrpt.Rwl);
                
            //}
            //this.SprintBurnDownChart.Series[0].Name = "Remaining Workload";
            //this.SprintBurnDownChart.Series[1].Name = "Estimated Total Workload";
            //this.SprintBurnDownChart.Series[2].Name = "Real Time Spent";

            #endregion



            //Alternate method with DataBind - MUCH BETTER
            #region Graph Method 2 : DataBind
            DataLayer.dlburndownchart dlburnd = new DataLayer.dlburndownchart();
            DataSet dtsgraph = dlburnd.myburndownchart(sprintid);
            
            SprintBurnDownChart.Series[0].XValueMember = "TimeStamp";
            SprintBurnDownChart.Series[0].YValueMembers = "YValue";
            SprintBurnDownChart.Series[0].LegendText = "Remaining Workload";

            SprintBurnDownChart.Series[1].XValueMember = "TimeStamp";
            SprintBurnDownChart.Series[1].YValueMembers = "EWL";
            SprintBurnDownChart.Series[1].LegendText = "Estimated Total Workload";

            SprintBurnDownChart.Series[2].XValueMember = "TimeStamp";
            SprintBurnDownChart.Series[2].YValueMembers = "RWL";
            SprintBurnDownChart.Series[2].LegendText = "Real Time Spent";

            SprintBurnDownChart.DataSource = dtsgraph;
            SprintBurnDownChart.DataBind();
            #endregion


            label1.Text = sprinttitel;

            DataLayer.dlsprintlist DLsprintlistfiller = new DataLayer.dlsprintlist();
            DataSet dts = DLsprintlistfiller.SprintList(sprintid, sprinttitel);

            string date1 = dts.Tables[0].Rows[0][5].ToString();
            date1= date1.Substring(0, date1.IndexOf(" "));
            string date2 = dts.Tables[0].Rows[0][6].ToString();
            date2= date2.Substring(0, date2.IndexOf(" "));
            label2.Text= date1;
            label8.Text = date2;
            textBox1.Text = dts.Tables[0].Rows[0][2].ToString();

            if (dtsgraph.Tables[0].Rows.Count > 0)
            {
                this.lblTEWL.Text = dtsgraph.Tables[0].Rows[0]["EWL"].ToString();
                int lastrow = dtsgraph.Tables[0].Rows.Count-1;
                this.lblTTS.Text = dtsgraph.Tables[0].Rows[lastrow]["RWL"].ToString();
            }

            string team = dts.Tables[0].Rows[0][8].ToString();
            
            DataLayer.dlteams DLTeamfiller = new DataLayer.dlteams();
            DataSet dtsteam = DLTeamfiller.TeamList(team);            
            label3.Text= dtsteam.Tables[0].Rows[0][1].ToString();

            #region Populatelist

            BusinnessLogic.Sprintview BLsprintview = new BusinnessLogic.Sprintview();
            List<UserStory> userstorylist = BLsprintview.PopulateUserstoryList(sprintid);

            foreach (UserStory userst in userstorylist)
            {
                ListViewItem lvitem = new ListViewItem();
                lvitem.Text = userst.Userstoryid.ToString();

                ListViewItem.ListViewSubItem nsubitem = new ListViewItem.ListViewSubItem();
                nsubitem.Text = userst.Titel;

                ListViewItem.ListViewSubItem nsubitem2 = new ListViewItem.ListViewSubItem();
                nsubitem2.Text = userst.Status;

                ListViewItem.ListViewSubItem nsubitem3 = new ListViewItem.ListViewSubItem();
                nsubitem3.Text = userst.Estimatedworkload.ToString();

                ListViewItem.ListViewSubItem nsubitem4 = new ListViewItem.ListViewSubItem();
                nsubitem4.Text = userst.Realworkload.ToString();

                ListViewItem.ListViewSubItem nsubitemremain = new ListViewItem.ListViewSubItem();
                nsubitemremain.Text = (userst.Estimatedworkload-userst.Realworkload).ToString();

                ListViewItem.ListViewSubItem nsubitemempty = new ListViewItem.ListViewSubItem();
                nsubitemempty.Text = null;

                lvitem.SubItems.Add(nsubitem);
                lvitem.SubItems.Add(nsubitemempty);  
                lvitem.SubItems.Add(nsubitemempty);
                lvitem.SubItems.Add(nsubitem2);
                lvitem.SubItems.Add(nsubitem3);
                lvitem.SubItems.Add(nsubitem4);
                lvitem.SubItems.Add(nsubitemremain);
                
                this.LVwUserStories.Items.Add(lvitem);

                BusinnessLogic.Sprintview BLsprint2view = new BusinnessLogic.Sprintview();
                List<Tasks> Taskslist = BLsprint2view.PopulateTasksList(userst.Userstoryid);

                foreach (Tasks task in Taskslist)
                {

                    ListViewItem lvitemTasks = new ListViewItem();
                    lvitemTasks.Text = task.TaskID.ToString();

                    ListViewItem.ListViewSubItem tsubitem = new ListViewItem.ListViewSubItem();
                    tsubitem.Text = task.Titel;

                    ListViewItem.ListViewSubItem tsubitemuser = new ListViewItem.ListViewSubItem();
                    tsubitemuser.Text = (task.Userfirstname+ " " + task.Username).ToString();

                    ListViewItem.ListViewSubItem tsubitem2 = new ListViewItem.ListViewSubItem();
                    tsubitem2.Text = task.Status;

                    ListViewItem.ListViewSubItem tsubitem3 = new ListViewItem.ListViewSubItem();
                    tsubitem3.Text = task.Estimatedworkload.ToString();

                    ListViewItem.ListViewSubItem tsubitem4 = new ListViewItem.ListViewSubItem();
                    tsubitem4.Text = task.RealWorkload.ToString();

                    ListViewItem.ListViewSubItem tsubitemremain = new ListViewItem.ListViewSubItem();
                    tsubitemremain.Text = (task.Estimatedworkload - task.RealWorkload).ToString();

                    ListViewItem.ListViewSubItem tsubitemempty = new ListViewItem.ListViewSubItem();
                    tsubitemempty.Text = null;


                    lvitemTasks.SubItems.Add(tsubitemempty);
                    lvitemTasks.SubItems.Add(tsubitem);  
                    lvitemTasks.SubItems.Add(tsubitemuser);
                    lvitemTasks.SubItems.Add(tsubitem2);
                    lvitemTasks.SubItems.Add(tsubitem3);
                    lvitemTasks.SubItems.Add(tsubitem4);
                    lvitemTasks.SubItems.Add(tsubitemremain);

                    this.LVwUserStories.Items.Add(lvitemTasks);
                }

            }
            #endregion


            
        }

        private void LVwUserStories_DoubleClick(object sender, EventArgs e)
        {
            bool itemselected = false;

            if (LVwUserStories.FocusedItem.SubItems[2].Text != "")
            {
                itemselected = true;
            }

            if (itemselected == true)
            {

                int tasksid = int.Parse(LVwUserStories.FocusedItem.Text.ToString());
                
                TasksDetailsForm ntasksdetailsform = new TasksDetailsForm(tasksid);
                ntasksdetailsform.Show();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
